//
//  VideoTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "VideoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation VideoTableViewCell {
    UIImageView* imgView;       //大图
    
    UILabel* titleLabel;
    UILabel* contentLabel;
    
    UILabel* videoTimeLabel;    //视频时长
    UILabel* playCountLabel;    //播放次数
    
    UIButton* penBtn;           //跟帖
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self _createSubViews];
    }
    
    return self;
}

- (void)_createSubViews {
    //大图
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 55, kWidth-20, 200)];
    [self.contentView addSubview:imgView];
    //大图上面的播放按钮
    UIButton* playBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [playBtn setImage:[UIImage imageNamed:@"video_list_cell_big_icon"] forState:UIControlStateNormal];
    playBtn.center = imgView.center;
    playBtn.alpha = 0.5;
    [playBtn addTarget:self action:@selector(playBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:playBtn];
    
    //标题
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, kWidth-20, 20)];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:titleLabel];
    //简介
    contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, kWidth-20, 20)];
    contentLabel.textColor = [UIColor grayColor];
    contentLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:contentLabel];
    
    //时长
    UIImageView* timeImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, imgView.bottom+15, 20, 20)];
    timeImgView.image = [UIImage imageNamed:@"video_list_cell_time"];
    [self.contentView addSubview:timeImgView];
    //
    videoTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, imgView.bottom+15, 40, 20)];
    videoTimeLabel.textColor = [UIColor grayColor];
    videoTimeLabel.textAlignment = NSTextAlignmentLeft;
    videoTimeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:videoTimeLabel];
    
    //播放次数
    UIImageView* playCountImgView = [[UIImageView alloc] initWithFrame:CGRectMake(70, imgView.bottom+15, 20, 20)];
    playCountImgView.image = [UIImage imageNamed:@"video_list_cell_count"];
    [self.contentView addSubview:playCountImgView];
    //
    playCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, imgView.bottom+15, 40, 20)];
    playCountLabel.textColor = [UIColor grayColor];
    playCountLabel.textAlignment = NSTextAlignmentLeft;
    playCountLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:playCountLabel];
    
    //右下角的两个按钮
    penBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth-80, imgView.bottom+10, 30, 30)];
    [penBtn setImage:[UIImage imageNamed:@"video_comment_pen"] forState:UIControlStateNormal];
    penBtn.layer.cornerRadius = 15;
    penBtn.layer.borderColor = [UIColor colorWithRed:180.0/255 green:180.0/255 blue:180.0/255 alpha:1].CGColor;
    penBtn.layer.borderWidth = 0.5;
    penBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [penBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [penBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
    [penBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 0, 0)];
    [self.contentView addSubview:penBtn];
    //
    UIButton* shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth-40, imgView.bottom+10, 30, 30)];
    [shareBtn setImage:[UIImage imageNamed:@"video_category_share"] forState:UIControlStateNormal];
    shareBtn.layer.cornerRadius = 15;
    shareBtn.layer.borderColor = [UIColor colorWithRed:180.0/255 green:180.0/255 blue:180.0/255 alpha:1].CGColor;
    shareBtn.layer.borderWidth = 0.5;
    [self.contentView addSubview:shareBtn];
}

- (void)playBtnAction {
    NSString* mp4Url = _videoModel.mp4Url;
    if (mp4Url.length) {
        //根据响应者链找到控制器类，push到要观看的视频
        UIViewController* vc = [Tools className:@"UIViewController" fromResponder:self.nextResponder];
        if (vc) {
            MPMoviePlayerViewController* playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:mp4Url]];
            [vc.navigationController pushViewController:playerVC animated:YES];
            vc.tabBarController.tabBar.hidden = YES;
        }
    }
}

- (void)setVideoModel:(VideoModel *)videoModel {
    if (_videoModel != videoModel) {
        _videoModel = videoModel;
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //标题
    titleLabel.text = _videoModel.title;
    
    //简介
    contentLabel.text = _videoModel.videoDescription;
    
    //大图
    NSString* imgUrl = _videoModel.coverImgUrl;
    [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    
    //时长
    NSInteger timeLength = [_videoModel.videoTimeLength integerValue];
//    NSInteger hh = timeLength/3600;
    videoTimeLabel.text = [NSString stringWithFormat:@"%li", timeLength];
    
    //播放次数
    CGFloat playCount = [_videoModel.playCount floatValue];
    if (playCount > 9999) {
        playCountLabel.text = [NSString stringWithFormat:@"%.1f万", playCount/10000];
    } else {
        playCountLabel.text = [NSString stringWithFormat:@"%.0f", playCount];
    }
    
    //跟帖数
    NSInteger replyCount = [_videoModel.playCount integerValue];
    NSString* replyStr = [NSString stringWithFormat:@"%li", replyCount];
    CGFloat width = replyStr.length*5+30+20;
    CGRect frame = penBtn.frame;
    frame.origin.x = kWidth-50-width;
    frame.size.width = width;
    [penBtn setFrame:frame];
    [penBtn setTitle:replyStr forState:UIControlStateNormal];
}

@end
